package com.itbaima.edu.info.manager.controller;

import com.itbaima.edu.info.manager.domain.Student;
import com.itbaima.edu.info.manager.domain.Teacher;
import com.itbaima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

/**
 * Created by byakuya on 2020/8/5
 */
public class TeacherController {

    private final Scanner sc = new Scanner(System.in);
    private final TeacherService teacherService = new TeacherService();

    public void start() {
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:  //添加老师
                    addTeacher();
                    break;
                case 2:  //删除老师
                    deleteTeacherById();
                    break;
                case 3:  //修改老师
                    updateTeacher();
                    break;
                case 4:  //查看老师
                    findAllTeacher();
                    break;
                case 5:  //告辞
                    System.out.println("告辞");
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void updateTeacher() {
        //录入已经存在的ID
        String id = inputStudentId();
        //录入老师的新信息
        Teacher newTeacher = inputStudentInfo(id);
        //调用业务员的修改方法
        teacherService.updateTeacher(id, newTeacher);
        System.out.println("修改成功！");
    }

    public void deleteTeacherById() {
        //录入待删除老师
        String delId = inputStudentId();
        //调用业务员删除方法
        teacherService.deleteTeacherById(delId);
        System.out.println("删除成功！");
    }

    public void findAllTeacher() {
        //从业务员中获取老师对象数组
        Teacher[] teachers = teacherService.findAllTeacher();
        //判断数组中是否有元素
        if (teachers == null){
            System.out.println("查无信息，请添加后重试");
        }else {
            System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
            for (Teacher t : teachers) {
                if (t != null)
                    System.out.println(t.getId() + "\t" + t.getName() + "\t\t" + t.getAge() + "\t" + t.getBirthday());
            }
        }
    }

    public void addTeacher() {
        String id;
        while (true) {  //判断不存在的老师id
            System.out.println("请输入老师id");
            id = sc.next();
            //判断是否存在
            if (teacherService.isExists(id))
                System.out.println("id已被占用，请重新输入");
            else break;
        }
        //接收老师的其它信息
        Teacher t = inputStudentInfo(id);
        //将封装好的老师对象传递给TeacherService
        if (teacherService.addTeacher(t)){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    //键盘录入已经存在的学生id
    public String inputStudentId() {
        String id;
        while (true) {
            //键盘录入待删除ID。判断是否存在
            System.out.println("请输入老师ID");
            id = sc.next();
            //判断是否存在
            if (!teacherService.isExists(id)) {
                System.out.println("ID不存在，请重新输入");
            }else break;
        }
        return id;
    }

    //键盘录入老师信息，封装为老师对象
    public Teacher inputStudentInfo(String id) {
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        int age = sc.nextInt();
        System.out.println("请输入老师生日");
        String birthday = sc.next();

        Teacher t = new Teacher();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        t.setBirthday(birthday);
        return t;
    }
}
